package com.project.myxima.presenters;

import com.project.myxima.data.XimalayaApi;
import com.project.myxima.interfaces.IAlbumDetailPresenter;
import com.project.myxima.interfaces.IAlbumDetailViewCallback;
import com.ximalaya.ting.android.opensdk.datatrasfer.IDataCallBack;
import com.ximalaya.ting.android.opensdk.model.album.Album;
import com.ximalaya.ting.android.opensdk.model.track.Track;
import com.ximalaya.ting.android.opensdk.model.track.TrackList;

import java.util.ArrayList;
import java.util.List;

public class AlbumDetailPresenter implements IAlbumDetailPresenter {

    private static final String TAG = "AlbumDetailPresenter";
    private Album mTargetAlbum = null;
    private List<IAlbumDetailViewCallback> mCallbacks = new ArrayList<>();
    private int mAlbumID;
    private int mCurrentPage = -1;
    private List<Track> mTracks = new ArrayList<>();

    private AlbumDetailPresenter(){}
    private static AlbumDetailPresenter sInstance = null;
    public static AlbumDetailPresenter getInstance(){
        if (sInstance==null) {
            synchronized (AlbumDetailPresenter.class){
                if (sInstance==null){
                    sInstance = new AlbumDetailPresenter();
                }
            }
        }
        return sInstance;
    }



    @Override
    public void poll2RefreshMore() {
    }

    @Override
    public void loadMore() {
        //TODO: load more data
        mCurrentPage++;
        doLoad(true);
    }

    @Override
    public void getAlbumDetail(int Album_id, int page) {
        // save the album id and the page
        this.mAlbumID = Album_id;
        this.mCurrentPage = page;

        doLoad(false);

    }

    private void doLoad(final boolean isLoadMore) {
        XimalayaApi ximalayaApi = XimalayaApi.getXimalayaApi();
        ximalayaApi.getAlbumDetail(new IDataCallBack<TrackList>() {
            @Override
            public void onSuccess(TrackList trackList) {
                List<Track> tracks = trackList.getTracks();
                int size = tracks.size();
                if(isLoadMore){
                    mTracks.addAll(tracks);
                    handleLoadMoreResult(size);
                }else{
                    mTracks.addAll(0,tracks);
                }

                handleAlbumDetailResult(mTracks);
            }

            @Override
            public void onError(int ErrorCode, String ErrorMsg) {
                if(isLoadMore){
                    mCurrentPage--;
                }
                handleError(ErrorCode, ErrorMsg);
            }
        },mAlbumID,mCurrentPage);
    }

    private void handleLoadMoreResult(int size) {
        for(IAlbumDetailViewCallback callback:mCallbacks){
            callback.onLoadMoreFinish(size);
        }
    }

    private void handleError(int errorCode, String errorMsg) {
        if (mCallbacks != null) {
            for(IAlbumDetailViewCallback callback:mCallbacks){
                callback.onNetworkError(errorCode,errorMsg);
            }
        }
    }

    private void handleAlbumDetailResult(List<Track> tracks) {
        for (IAlbumDetailViewCallback callback:mCallbacks){
            callback.onDetailListLoaded(tracks);
        }
    }

    @Override
    public void registerViewCallback(IAlbumDetailViewCallback detailViewCallback) {
        if(!mCallbacks.contains(detailViewCallback)){
            mCallbacks.add(detailViewCallback);
            if (mTargetAlbum != null) {
                detailViewCallback.onAlbumLoaded(mTargetAlbum);
            }
        }
    }

    @Override
    public void unregisterViewCallback(IAlbumDetailViewCallback detailViewCallback) {
        if(mCallbacks.contains(detailViewCallback)){
            mCallbacks.remove(detailViewCallback);
        }

    }

    public void setTargetAlbum(Album targetAlbum){
        this.mTargetAlbum = targetAlbum;
    }
}
